package asia.kalin.service.impl;

import asia.kalin.domain.ao.Result;
import asia.kalin.domain.po.Department;
import asia.kalin.domain.po.UserInfo;
import asia.kalin.domain.vo.DepartmentManagerVO;
import asia.kalin.domain.vo.DepartmentUserVO;
import asia.kalin.enums.RedisKeyEnums;
import asia.kalin.mapper.DepartmentMapper;
import asia.kalin.mapper.UserInfoMapper;
import asia.kalin.service.IDepartmentService;
import asia.kalin.utils.ThreadLocalUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Kalin
 * @since 2025-03-10
 */
@Service
@RequiredArgsConstructor
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements IDepartmentService {

    private final StringRedisTemplate stringRedisTemplate;

    private final UserInfoMapper userInfoMapper;

    /**
     * 查询当前用户部门相关人员
     * @return Result
     */
    @Override
    public Result getDepartmentPersonnelById() {
        // 得到当前用户部门id
        Map<String,Object> claims = ThreadLocalUtil.get();
        String departmentId = (String) claims.get("departmentId");

        // 拼接key
        String key = RedisKeyEnums.DEPARTMENT_RELEVANT_PERSONNEL_KEY + departmentId;
        // 查询redis
        String object = stringRedisTemplate.opsForValue().get(key);
        // 是否为空
        if(StrUtil.isNotBlank(object)){
            // 转换类型
            List<DepartmentUserVO> departmentUserVOS = JSONUtil.toList(object, DepartmentUserVO.class);
            return new Result<>(0,"查询成功", departmentUserVOS);
        }

        // 查询数据库
        List<UserInfo> departmentPersonnel = userInfoMapper.selectList(new QueryWrapper<UserInfo>()
                .eq("department_id", departmentId)
        );

        if(departmentPersonnel.isEmpty()){
            return new Result<>(1,"部门ID错误",null);
        }

        // 转换类型
        List<DepartmentUserVO> departmentUserVOS = BeanUtil.copyToList(departmentPersonnel, DepartmentUserVO.class);
        // 转换成json
        String jsonStr = JSONUtil.toJsonStr(departmentUserVOS);
        // 写入redis
        stringRedisTemplate.opsForValue().set(key,jsonStr,RedisKeyEnums.DEPARTMENT_RELEVANT_PERSONNEL_TTL, TimeUnit.HOURS);

        return new Result<>(0,"查询成功", departmentUserVOS);
    }

    /**
     * 查询所有部门的负责人信息
     * @return Result
     */
    @Override
    public Result getDepartmentManagerAll() {
        // 拼接key
        String key = RedisKeyEnums.DEPARTMENT_MANAGER_ALL_KEY;
        // 查询redis
        String object = stringRedisTemplate.opsForValue().get(key);
        // 判断是否为空
        if(StrUtil.isNotBlank(object)){
            // Json转VO
            List<DepartmentManagerVO> departmentUserVOS = JSONUtil.toList(object, DepartmentManagerVO.class);
            return new Result<>(0,"查询成功",departmentUserVOS);
        }

        // 查询数据库
        // 查询出所有部门负责人
        List<DepartmentManagerVO> departmentUserVOS = baseMapper.getDepartmentManager();
        // 转换成JSON
        String jsonStr = JSONUtil.toJsonStr(departmentUserVOS);
        // 写入redis
        stringRedisTemplate.opsForValue().set(key,jsonStr,RedisKeyEnums.DEPARTMENT_MANAGER_ALL_TTL,TimeUnit.HOURS);

        return new Result<>(0,"查询成功",departmentUserVOS);
    }
}
